package com.dataart.productshop.dao.impl;

import com.dataart.productshop.dao.FileInfoDao;
import com.dataart.productshop.model.FileInfo;
import com.dataart.productshop.model.Type;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class FileInfoDaoImpl extends BaseDaoImpl<FileInfo> implements
		FileInfoDao {

	public FileInfoDaoImpl() {
		super(FileInfo.class);
	}

	@Override
	public List<FileInfo> getHundredByDownloads() {
		return getByDownloads(0, 20);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<FileInfo> getByDownloads(int from, int amount) {
		Criteria criteria = createCriteria(FileInfo.class).setFetchMode("pictures", FetchMode.JOIN);
		return criteria.addOrder(Order.desc("downloads"))
				.addOrder(Order.desc("createdDate")).setMaxResults(amount)
				.setFirstResult(from).list();
	}

	@Override
	public void saveFileInfo(FileInfo input) {
		save(input);
	}

	// check here
	@Override
	@SuppressWarnings("unchecked")
	public List<FileInfo> getByType(Type type) {
		return createCriteria(FileInfo.class)
				.add(Restrictions.eq("type", type)).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FileInfo> getByType(Type type, int from, int amount) {
		return createCriteria(FileInfo.class)
				.add(Restrictions.eq("type", type)).setMaxResults(amount)
				.setFirstResult(from).list();
	}
}
